// 用于测试并发服务器的客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define BUFFSIZE 4096

void processConnection(int clientFd);
int main(void)
{
    int clientFd;
    clientFd = socket(AF_INET, SOCK_STREAM, 0);
    if (clientFd > 0)
    {
        printf("socket created successed.\n");
    }
    else
    {
        printf("socket created failed.\n");
        exit(-1);
    }
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    inet_aton("127.0.0.1", &serverAddr.sin_addr);
    int error = connect(clientFd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
    if (error == 0)
    {
        printf("connect successed.\n");
    }
    else
    {
        printf("connect failed.\n");
        exit(-1);
    }
    processConnection(clientFd);
    return 0;
}
void processConnection(int clientFd)
{
    char buffer[BUFFSIZE] = {0};
    while (1)
    {
        // recv(clientFd, buffer, sizeof buffer, 0);
        // printf("%s", buffer);
        scanf("%s", buffer);
        printf("start send\n");
        send(clientFd, buffer, strlen(buffer)+1, 0);
        printf("start recv.\n");
        recv(clientFd, buffer, sizeof buffer, 0);
        printf("%s\n", buffer);
    }
}